// pages/group_forward/group_forward.js
var common = require("../../../common/common.js").common;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        choose: 1,
        money: 0,
        put_forward: "",
        service: 0,
        serviceType: 0,
        service_money: 0,
        payee_name: "",
        account1: "",
        account2: "",
        info: {
            alipay: 0,
            //支付宝
            blank: 0,
            //y银行
            wechatOnline: 0,
            //微信线上
            wechatOffline: 0
        },
        payBtn: false,
        bankName: ""
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            money: options.money
        });
        this.readInfo();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    // onShareAppMessage: function () {
    // },
    choose: function(e) {
        var id = e.currentTarget.dataset.id;
        this.setData({
            choose: id
        });
    },
    putmoney: function(e) {
        var money = e.detail.value;
        var unit = this.data.infoSet.unit;
        var service_money = this.data.infoSet.draw_fee;
        if (unit == 2) {
            service_money = service_money;
            console.log("提现手续固定金额:" + service_money);
        } else {
            if (money == "") {
                money = 0;
            }
            service_money = parseFloat(money) * parseFloat(service_money / 100);
            console.log("提现手续百分比：" + service_money / 100);
            if (money == 0) {
                money = "";
            }
        }
        this.setData({
            put_forward: money,
            service_money: Number(service_money).toFixed(2)
        });
    },
    all: function(e) {
        var money = e.currentTarget.dataset.money;
        var unit = this.data.infoSet.unit;
        var service_money = this.data.infoSet.draw_fee;
        if (unit == 2) {
            service_money = service_money;
            console.log("提现手续固定金额:" + service_money);
        } else {
            service_money = parseFloat(money) * parseFloat(service_money / 100);
            console.log("提现手续百分比：" + service_money / 100);
        }
        this.setData({
            put_forward: money,
            service_money: Number(service_money).toFixed(2)
        });
    },
    getPayeeName: function(e) {
        this.setData({
            payee_name: e.detail.value
        });
    },
    getAccount1: function(e) {
        this.setData({
            account1: e.detail.value
        });
    },
    getAccount2: function(e) {
        this.setData({
            account2: e.detail.value
        });
    },
    getBankName(e) {
        this.setData({
            bankName: e.detail.value
        });
    },
    putForward: function(e) {
        var openid = wx.getStorageSync("user").openid;
        var that = this;
        var payBtn = that.data.payBtn;
        if (payBtn) {
            return false;
        }
        var put_forward = that.data.put_forward;
        var payee_name = that.data.payee_name;
        var type = that.data.choose;
        var account = "";
        var all_money = that.data.money;
        let form_id = e.detail.formId;
        that.setData({
            payBtn: true
        });
        if (parseFloat(put_forward) > parseFloat(all_money)) {
            wx.showModal({
                title: "提示",
                content: "提现金额超出可提现佣金！",
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            payBtn: false
                        });
                    }
                }
            });
            return false;
        }
        var param = {};
        if (type == 1) {
            //支付宝
            param.account = that.data.account1;
        }
        if (type == 2) {
            //银行卡
            param.name = that.data.payee_name;
            param.account = that.data.account2;
            param.bank = that.data.bankName;
        }
        if (param.account == "" && type == 1) {
            wx.showModal({
                title: "提示",
                content: "请填写支付宝账号！",
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            payBtn: false
                        });
                    }
                }
            });
            return false;
        }
        if (param.name == "" && type == 2) {
            wx.showModal({
                title: "提示",
                content: "请填写银行卡姓名！",
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            payBtn: false
                        });
                    }
                }
            });
            return false;
        }
        if (param.account == "" && type == 2) {
            wx.showModal({
                title: "提示",
                content: "请填写银行卡号！",
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            payBtn: false
                        });
                    }
                }
            });
            return false;
        }
        param.draw_money = put_forward;
        param.draw_way = type;
        common.request({
            endpoint: "leader-area/apply-draw",
            method: "post",
            data: param,
            header: {
                "content-type": "application/json"
            },
            success: res => {
                console.log(res);
                wx.showModal({
                    title: "提示",
                    content: "已提交审核，请耐心等待！"
                });
                setTimeout(() => {
                    wx.redirectTo({
                        url: "/areaLeader/pages/myWallet/myWallet"
                    });
                }, 1500);
            },
            fail: res => {
                console.log(res);
                wx.showModal({
                    title: "提示",
                    content: res.message,
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            that.setData({
                                payBtn: false
                            });
                        }
                    }
                });
            }
        });
    },
    getService: function() {
        var that = this;
        common.request({
            endpoint: "Apply/financeapply/brokerage",
            method: "post",
            header: {
                "content-type": "application/json"
            },
            success: res => {
                console.log(res);
                that.setData({
                    service: res.extra.money_service,
                    serviceType: res.extra.type
                });
            },
            fail: res => {}
        });
    },
    readInfo: function() {
        var that = this;
        common.request({
            endpoint: "leader-area/leader-area-set-read",
            method: "post",
            data: {},
            success: res => {
                var payWay = res.extra.draw_way;
                that.dealWay(payWay);
                that.setData({
                    infoSet: res.extra
                });
            }
        });
    },
    dealWay: function(payWay) {
        let that = this;
        var payWay = payWay.split(",");
        let info = that.data.info;
        payWay.forEach((item, index) => {
            switch (item) {
              case "1":
                info.alipay = 1;
                break;

              case "2":
                info.blank = 1;
                break;

              case "4":
                info.wechatOnline = 1;
                break;

              case "3":
                info.wechatOffline = 1;
                break;
            }
        });
        that.setData({
            info: info,
            choose: payWay[0]
        });
    }
});